<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!-- 静态资源文件 -->
<%@ include file="../head.jsp"%>
<link type="text/css" rel="stylesheet" href="./static/zTree/2.6/zTreeStyle.css"/>
	
<script type="text/javascript" src="./static/zTree/2.6/jquery.ztree-2.6.min.js"></script>
<title>角色管理</title>
<style>
.div-top {
	margin-top: 10px;
}
</style>
</head>
<script type="text/javascript">
	/*绑定 树形*/
	var zTree ;
	function funcBindTree(){
		if(ZD.ux.SelectedCheckboxCount("table_info") != 1){
			ZD.msg.alert("请选择一条记录进行操作.");
			return;
		}
		var fid = ZD.ux.SelectedCheckboxIds("table_info");
		ZD.ux.Ajax({
			data:{role_id:fid, method:'finance.sys.role.getRightByRoleId'},
			success:function(res){
				var setting = {
				    showLine: true,
				    checkable: true,
				    showIcon :true
				};
				zTree = $("#tree").zTree(setting, res.data);
				var nodes = zTree.getNodes();
	            for (var i = 0; i < nodes.length; i++) {
	            	zTree.expandNode(nodes[i], true, false, true);
	            }
	            $("#permissionModalLabel").text("菜单权限");
	    		$("#permissionModal").modal("show");
	    		$("#savePerBtn").show();
			}
		});
	}
	
	/*绑定网格*/
	function funcBindGrid() {
		var fkey = $("#fkey").val().trim();
		ZD.ux.BindGrid({
			data : {
				currpage : 1,
				'count' : '10',
				Key : fkey,
				method : 'finance.sys.role.findRoleList'
			},
			table : "dvlist"
		});
	}
	/*增加用户*/
	function func_add() {
		$("#myModalLabel").text("新增");
		$("#myModal").modal("show");
		$("#saveBtn").show();
		$("#myModal").find("input").attr("readOnly", false); //不可编辑，可以传值
		$("#myModal").find("select").removeAttr("disabled");
		ZD.ux.ClearInputValue("#myModal");
	}

	/*修改用户*/
	function func_update() {
		if (ZD.ux.SelectedCheckboxCount("table_info") != 1) {
			ZD.msg.alert("请选择一条记录进行操作.");
			return;
		}
		var fid = ZD.ux.SelectedCheckboxIds("table_info");
		$("#myModalLabel").text("修改");
		ZD.ux.Ajax({
			data : {
				id : fid,
				method : 'finance.sys.role.get'
			},
			success : function(res) {
				if (res.status == 0) {
					ZD.ux.InitInputValue(res.data);
					$("#myModal").find("input").attr("readOnly", false); //不可编辑，可以传值
					$("#myModal").find("select").removeAttr("disabled");
					$("#myModal").modal("show");
				}
			}
		});

		$("#myModal").modal("show");
		$("#saveBtn").show();
	}

	/*删除用户*/
	function func_delete() {
		if (ZD.ux.SelectedCheckboxCount("table_info") != 1) {
			ZD.msg.alert("请选择一条记录进行操作.");
			return;
		}
		var fid = ZD.ux.SelectedCheckboxIds("table_info");
		ZD.msg.confirm("您确定要删除勾选的[" + ZD.ux.SelectedCheckboxCount("table_info")
				+ "]条记录吗？", function() {
			ZD.ux.Ajax({
				data : {
					id : fid,
					method : 'finance.sys.role.deleteRole'
				},
				success : function(res) {
					if (res.status == 0) {
						funcBindGrid();
					} else {
						ZD.msg.alert("操作失败:" + res.msg);
					}
				}
			});
		});
	}

	/*保存授权操作*/
	function func_saveright(){
		var nodes = zTree.getCheckedNodes();
		var tmpNode;
		var fids = "";
		for(var i=0; i<nodes.length; i++){
			tmpNode = nodes[i];
			if(fids.length == 0){
				fids = tmpNode.id;
			}else{
				fids += ','+tmpNode.id;
			}
		}
		var fid = ZD.ux.SelectedCheckboxIds("table_info");
		ZD.ux.Ajax({
			data:{fids:fids,role_id:fid, method:'finance.sys.role.SaveRoleRightInfo'},
			success:function(res){
				if(res.status == 0){
					ZD.msg.alert("操作成功.");
					$("#permissionModal").modal("hide");
				}else{
					ZD.msg.alert("操作失败.");
				}
			}
		});
	}
	/*保存记录*/
	function func_save() {
		/*正则验证*/
		if (!ZD.ux.Verify()) {
			return false;
		}
		/*遍历标签下的所有input标签*/
		var fdata = ZD.ux.GetInputValue("#myModal");
		if (fdata.id.length > 0) {
			fdata.method = 'finance.sys.role.update';
		} else {
			fdata.method = 'finance.sys.role.add';
		}

		/*新增或编辑*/
		ZD.ux.Ajax({
			data : fdata,
			success : function(res) {
				if (res.status == 0) {
					$("#myModal").modal("hide");
					funcBindGrid();
				} else {
					ZD.msg.alert("操作失败:" + res.msg);
				}
			}
		});
	}

	/*查看*/
	function func_read() {
		if (ZD.ux.SelectedCheckboxCount("table_info") != 1) {
			ZD.msg.alert("请选择一条记录进行操作.");
			return;
		}
		var fid = ZD.ux.SelectedCheckboxIds("table_info");
		$("#myModalLabel").text("查看");
		ZD.ux.Ajax({
			data : {
				id : fid,
				method : 'finance.sys.role.get'
			},
			success : function(res) {
				if (res.status == 0) {
					ZD.ux.InitInputValue(res.data);
					console.log('readOnly');
					$("#myModal").modal("show");
					$("#myModal").find("input").attr("readOnly", true); //不可编辑，可以传值
					$("#myModal").find("select").attr("disabled", "disabled");
				}
			}
		});

		$("#myModal").modal("show");
		$("#saveBtn").hide();
	}

	$(function() {
		/*绑定数据*/
		funcBindGrid();

	});
</script>
<body>
	<div class="panel panel-primary">
		<div class="panel-heading">角色管理</div>
		<div class="panel-body">
			<!-- 搜索面板 -->
			<div style="width: 200px;">
				<div class="input-group">
					<input type="text" class="form-control" id="fkey"> <span
						class="input-group-btn">
						<button class="btn btn-default" type="button"
							onclick="funcBindGrid();">搜索</button>
					</span>
				</div>
			</div>

			<!-- 按钮组 -->
			<div class="btn-toolbar div-top">
				<div class="btn-group">
					<button class="btn" onclick="func_read();">
						<i class="icon icon-file-text-o">查看</i>
					</button>
				</div>
				<div class="btn-group">
					<button class="btn" onclick="func_update();">
						<i class="icon icon-save">修改</i>
					</button>
				</div>

				<div class="btn-group">
					<button class="btn" class="btn btn-lg btn-primary"
						onclick="func_add();">
						<i class="icon icon-plus">增加</i>
					</button>
				</div>
				<div class="btn-group">
					<button class="btn" onclick="funcBindTree();">
						<i class="icon icon-lock">菜单权限</i>
					</button>
				</div>
				<div class="btn-group">
					<button class="btn" onclick="func_delete();">
						<i class="icon icon-minus">删除</i>
					</button>
				</div>
			</div>

			<!--表格控件 -->
			<table class="table table-hover div-top" id="table_info">
				<thead>
					<tr>
						<th>#</th>
						<th>角色名称</th>
						<th>英文名称</th>
						<th>状态</th>
					</tr>
				</thead>
				<tbody id="dvlist">
					<tr>
						<td class="center" style="width: 50px;"><label><input
								type='checkbox' name='ids' value="{{id}}" alt="{{id}}" /><span
								class="lbl"></span></label></td>
						<td>{{role_name}}</td>
						<td>{{role_eng}}</td>
						<td>{{statusName}}</td>
					</tr>
				</tbody>
			</table>

		</div>
	</div>

	<!-- ...编辑的弹框... -->
	<div class="modal fade" id="myModal" tabindex="-1" User="dialog"
		aria-labelledby="myModalLabel">
		<div class="modal-dialog" User="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">{title}</h4>
				</div>
				<div class="modal-body">
					<input type="hidden" id="id" />
					<div class="form-group">
						<label for="txt_parentdepartment">角色名称</label> <input type="text"
							class="form-control" id="role_name" placeholder="角色名称"
							data-verify="Empty" data-verify_text="角色名称不能为空">
					</div>
					<div class="form-group">
						<label for="txt_parentdepartment">英文名称</label> <input type="text"
							class="form-control" id="role_eng" placeholder="英文名称">
					</div>
					<div class="form-group">
						<label for="txt_departmentlevel">备注</label> <input type="text"
							class="form-control" id="remark" placeholder="备注">
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" class="btn btn-primary"
							onclick="func_save();" id="saveBtn">保存</button>
					</div>
				</div>
			</div>
		</div>
	</div>


	<!-- ...权限菜单的弹框... -->
	<div class="modal fade" id="permissionModal" tabindex="-1"
		User="dialog" aria-labelledby="permissionModalLabel">
		<div class="modal-dialog" User="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="permissionModalLabel">{title}</h4>
				</div>
				<div class="modal-body">
					<ul id="tree" class="tree" style="overflow:auto;"></ul>
				</div>		
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" class="btn btn-primary"
							onclick="func_saveright();" id="savePerBtn">保存</button>
					</div>
				</div>
			</div>
		</div>

	
</body>
</html>